﻿using P2P.DB.Model;
using SqlSugar;

namespace P2P.DB
{
    public class SqliteHelper
    {
        private static readonly string dbPath = "P2P.db";

        public static SqlSugarClient GetDb() 
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = $"Data Source={dbPath}",
                DbType = DbType.Sqlite,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
                MoreSettings = new ConnMoreSettings()
                {
                    IsAutoRemoveDataCache = true
                }
            });

            if (!File.Exists(dbPath)) 
            {
                db.DbMaintenance.CreateDatabase();
                db.CodeFirst.InitTables<FileModel>();
                db.CodeFirst.InitTables<ServerModel>();
            }
            return db;
        }
    }
}
